// pages/smartlife/bus/busline/busline.js
var common = require('../../../../utils/common.js');
var needRefresh = false
Page({
  data: {
    busRouteDetail: {},
    startStationName: '',
    endStationName: '',
    upDown: '',
    lineId: '',
    lineName: ''
  },
  onLoad: function (options) {
    // console.log(options.lineId+options.lineName+options.upDown+options.startStationName+options.endStationName)
    var that = this
    wx.setNavigationBarTitle({
      title: options.lineName
    })
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 10000
    })
    that.setData({
      startStationName: options.startStationName,
      endStationName: options.endStationName,
      upDown: options.upDown,
      lineId: options.lineId,
      lineName: options.lineName
    })
    wx.request({
      url: common.busUrl() + 'lineSearch/lineGps',
      data: {
        lineId: options.lineId,
        upOrDown: options.upDown,
        sid: -1,
        order: -1,
        userId: "",
        isUpdateCook: 1
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      success: function (res) {
        console.log(res)
        if (res.data) {
          // var busRouteDetail = res.data.data[0]
          // var runList = res.data.data[0].runList
          // // console.log(runList)
          // var uniqueLunList = that.unique(runList)
          // // console.log(uniqueLunList)
          // busRouteDetail.runList = uniqueLunList
          that.setData({
            busRouteDetail: res.data
          })
        } else {

        }
        wx.hideToast()
      }
    })
  },
  onShareAppMessage: function () {
    var that = this
    return {
      title: that.data.lineName + '：' + that.data.startStationName + '→' + that.data.endStationName,
      path: '/pages/smartlife/bus/busline/busline?lineId=' + that.data.lineId + '&lineName=' + that.data.lineName + '&upDown=' + that.data.upDown + '&startStationName=' + that.data.startStationName + '&endStationName=' + that.data.endStationName
    }
  },
  onShow: function () {
    if (needRefresh) {
      this.refresh()
    }
  },
  onHide: function () {
    needRefresh = true
  },
  reverse: function () {
    var that = this
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 10000
    })
    var upDown = that.data.upDown == 0 ? 1 : 0
    that.setData({
      startStationName: that.data.endStationName,
      endStationName: that.data.startStationName,
      upDown: upDown
    })
    wx.request({
      url: common.busUrl() + 'lineSearch/lineGps',
      data: {
        lineId: that.data.lineId,
        upOrDown: that.data.upDown,
        sid: -1,
        order: -1,
        userId: "",
        isUpdateCook: 1
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      success: function (res) {
        console.log(res)
        if (res.data) {
          // var busRouteDetail = res.data.data[0]
          // var runList = res.data.data[0].runList
          // // console.log(runList)
          // var uniqueLunList = that.unique(runList)
          // // console.log(uniqueLunList)
          // busRouteDetail.runList = uniqueLunList
          that.setData({
            busRouteDetail: res.data
          })
        } else {

        }
        wx.hideToast()
      }
    })
  },
  refresh: function () {
    var that = this
    wx.showToast({
      title: '加载中',
      icon: 'loading',
      duration: 10000
    })
    wx.request({
      url: common.busUrl() + 'lineSearch/lineGps',
      data: {
        lineId: that.data.lineId,
        upOrDown: that.data.upDown,
        sid: -1,
        order: -1,
        userId: "",
        isUpdateCook: 1
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      success: function (res) {
        console.log(res)
        if (res.data) {
          // var busRouteDetail = res.data.data[0]
          // var runList = res.data.data[0].runList
          // // console.log(runList)
          // var uniqueLunList = that.unique(runList)
          // // console.log(uniqueLunList)
          // busRouteDetail.runList = uniqueLunList
          that.setData({
            busRouteDetail: res.data
          })
        } else {

        }
        wx.hideToast()
      }
    })
  },
  mapClick: function (e) {
    var that = this
    wx.navigateTo({
      url: '/pages/smartlife/bus/busline/buslinemap/buslinemap?upDown=' + that.data.upDown + '&lineId=' + that.data.lineId + '&lineName=' + that.data.lineName
    })
  },
  stationClick: function (e) {
    var that = this
    var lat = that.data.busRouteDetail.relationStations[e.currentTarget.dataset.index].location.latitude
    var lgt = that.data.busRouteDetail.relationStations[e.currentTarget.dataset.index].location.longtitude
    wx.navigateTo({
      url: '/pages/smartlife/bus/busline/buslinemap/buslinemap?upDown=' + that.data.upDown + '&lineId=' + that.data.lineId + '&lat=' + lat + '&lgt=' + lgt + '&lineName=' + that.data.lineName
    })
  }
})